package com.netflexitysolutions.amazonws.sdb.orm.filter;

import com.netflexitysolutions.amazonws.sdb.orm.metadata.SimpleDBMapping;

/**
 * A simple Filter implementation that makes sure that a record is of a given type.
 * 
 * @author netflexity
 *
 */
public class TypeFilter<T> extends Predicate<T> {

    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private Class<T> type;
    private StringBuffer query = new StringBuffer(96);
    
    /**
     * @param type
     * @param mapping
     */
    public TypeFilter(Class<T> type, SimpleDBMapping mapping) {
        this.type = type;
        assert(type != null);
    }

    /* (non-Javadoc)
     * @see com.netflexitysolutions.amazonws.sdb.orm.filter.Filter#filter(java.lang.Object)
     */
    public boolean filter(T record) {
        if (record == null)
            return true;
        if (type == null) {
            return false;
        }
        return !record.getClass().equals(getType());
    }

	/* (non-Javadoc)
	 * @see com.netflexitysolutions.amazonws.sdb.orm.filter.Filter#getType()
	 */
	public Class<T> getType() {
		return this.type;
	}

	/* (non-Javadoc)
	 * @see com.netflexitysolutions.amazonws.sdb.orm.filter.Filter#toSelect()
	 */
	public String toSelect() {
		return query.toString();
	}

	/* (non-Javadoc)
	 * @see com.netflexitysolutions.amazonws.sdb.orm.filter.Predicate#getComplement()
	 */
	@Override
	public Predicate<T> getComplement() {
		return null;
	}
}
